package com.czh.auth.application.controller;

import com.czh.auth.application.convert.AuthRolePermissionConverter;
import com.czh.auth.application.dto.AuthRolePermissionDTO;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.auth.domain.service.AuthRolePermissionDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author caozaihui
 * @version 1.0
 * @since 2024/8/30 下午 10:00
 */
@RestController
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    @PostMapping("add")
    public ResponseResult<Boolean> addRoleWithPermission(@RequestBody AuthRolePermissionDTO authRolePermissionDTO){
        try {
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),"角色id不能为空!");
            Preconditions.checkNotNull(authRolePermissionDTO.getPermissionIdList(),"权限id不能为空!");
            return ResponseResult.success(authRolePermissionDomainService.addAuthRolePermission(AuthRolePermissionConverter.INSTANCE.convert(authRolePermissionDTO)));
        } catch (Exception e) {
            return ResponseResult.error("角色关联权限失败!");
        }
    }
}
